package com.weiyin.examination.application;

import android.app.Activity;
import android.app.Application;

import com.mob.MobApplication;

import java.util.ArrayList;

/**
 * @author njf
 *         Created by lenovo on 2017/9/21.
 */

public class App extends MobApplication {
    /**
     * 实例化一个app
     */
    public static App instance;
    private ArrayList<Activity> activityStack;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        activityStack = new ArrayList<>();
        CrashHandler.getInstance().init(this);
    }

    /**
     * 将应用程序的任务栈中的一activity实例添加到activityStack中
     *
     * @param activity activity
     */
    public void addActivity2Stack(Activity activity) {
        activityStack.add(activity);
    }

    /**
     * 经activity实例从activityStack中移除
     *
     * @param activity activity
     */
    public void removeActivityFromStack(Activity activity) {
        activityStack.remove(activity);
    }

    public static App getInstance() {
        return instance;
    }

    public void exit() {
        try {
            for (Activity activity : activityStack) {
                if (activity != null) {
                    activity.finish();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }
}
